﻿Option Strict On

Imports Sunts.DataLayer.SQLite
Imports System.Data.SQLite

Public Class VirtualMachine
    Public Property VirtualMachineId As Int32
    Public Property HostID As Int32
    Public Property VirtualMachineName As String
    Public Property VirtualMachineGuid As String

    Public Function Insert(ByVal ConnectionString As String) As Int32
        Try
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "INSERT INTO VirtualMachines (HostID, VirtualMachineName, VirtualMachineGuid) VALUES (@HostID, @VirtualMachineName, @VirtualMachineGuid )"

                sdl.ParameterCollection = New SQLiteParamCollection
                sdl.ParameterCollection.Add(New SQLiteParameter("@HostID", Me.HostID))
                sdl.ParameterCollection.Add(New SQLiteParameter("@VirtualMachineName", Me.VirtualMachineName))
                sdl.ParameterCollection.Add(New SQLiteParameter("@VirtualMachineGuid", Me.VirtualMachineGuid))

                Insert = sdl.ExecuteNonQuery()
            End Using
        Catch ex As Exception
            Throw New Exception("VirtualMachine: Insert()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        End Try

    End Function
    Public Shared Function SelectAll(ConnectionString As String) As VirtualMachineCollection
        Dim VirtualMachineCollection As VirtualMachineCollection
        Dim VirtualMachine As VirtualMachine
        Dim Reader As SQLiteDataReader

        Try
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "SELECT VirtualMachineId, HostID, VirtualMachineName, VirtualMachineGuid FROM VirtualMachines"

                VirtualMachineCollection = New VirtualMachineCollection

                Reader = sdl.ExecuteDataReader

                While Reader.Read
                    VirtualMachine = New VirtualMachine
                    VirtualMachine.VirtualMachineId = CInt(Reader("VirtualMachineId"))
                    VirtualMachine.HostID = CInt(Reader("HostID"))
                    VirtualMachine.VirtualMachineName = CStr(Reader("VirtualMachineName"))
                    VirtualMachine.VirtualMachineGuid = CStr(Reader("VirtualMachineGuid"))
                    VirtualMachineCollection.Add(VirtualMachine)
                End While
                Reader.Close()
            End Using

            SelectAll = VirtualMachineCollection

        Catch ex As Exception
            Throw New Exception("VirtualMachine: SelectAll()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        Finally
            VirtualMachine = Nothing
            VirtualMachineCollection = Nothing
            Reader = Nothing
        End Try
    End Function

    Public Shared Function SelectByID(VirtualMachineID As Int32, ConnectionString As String) As VirtualMachine
        Dim VirtualMachine As VirtualMachine
        Dim Reader As SQLiteDataReader

        Try
            VirtualMachine = New VirtualMachine
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "SELECT * FROM VirtualMachines WHERE VirtualMachineID = @VirtualMachineID"

                sdl.ParameterCollection = New SQLiteParamCollection
                sdl.ParameterCollection.Add(New SQLiteParameter("@VirtualMachineID", VirtualMachineID))

                Reader = sdl.ExecuteDataReader

                While Reader.Read
                    VirtualMachine = New VirtualMachine
                    VirtualMachine.VirtualMachineId = CInt(Reader("VirtualMachineId"))
                    VirtualMachine.HostID = CInt(Reader("HostID"))
                    VirtualMachine.VirtualMachineName = CStr(Reader("VirtualMachineName"))
                    VirtualMachine.VirtualMachineGuid = CStr(Reader("VirtualMachineGuid"))
                End While
                Reader.Close()
            End Using

            SelectByID = VirtualMachine

        Catch ex As Exception
            Throw New Exception("VirtualMachine: SelectByID()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        Finally
            VirtualMachine = Nothing
            Reader = Nothing
        End Try
    End Function

End Class

Public Class VirtualMachineCollection
    Inherits System.Collections.ObjectModel.Collection(Of VirtualMachine)

    Public Function ContainsVirtualMachine(ByVal VirtualMachineName As String) As Boolean
        Dim iEnum = Me.GetEnumerator

        While iEnum.MoveNext()
            Dim VirtualMachine As VirtualMachine = iEnum.Current
            If VirtualMachine.VirtualMachineName = VirtualMachineName Then Return True
        End While

        Return False

    End Function
    Public Function GetVirtualMachine(ByVal VirtualMachineName As String, ByVal HostID As Int32) As VirtualMachine
        Dim iEnum = Me.GetEnumerator

        While iEnum.MoveNext()
            Dim VirtualMachine As VirtualMachine = iEnum.Current
            If VirtualMachine.VirtualMachineName = VirtualMachineName AndAlso VirtualMachine.HostID = HostID Then Return VirtualMachine
        End While

        Return Nothing

    End Function
End Class
